package com.admin.utils;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author : alex
 * @date : 2020/3/9
 */
public class ValidatorUtils {
    private static Validator validator;

    public static void validateWithException(Object object) {
        Set<ConstraintViolation<Object>> set = validator.validate(object);
        if (set.size() > 0) {
            throw new ConstraintViolationException(set);
        }
    }

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }
}
